import * as dto from './DTO';
import {request, handleFetchResultNotification, REQUEST_TYPE} from '../utils';
import stringify from 'Shared/utils/stringify';
import {formatMessage} from './intl';
const baseUrl = '/sales/api/v1';

const getInitDesc = formatMessage({
    id: 'api.message.getInitDesc',
    defaultMessage: '初始化信息'
});
const getListDesc = formatMessage({
    id: 'api.message.getListDesc',
    defaultMessage: '查询潜客信息列表'
});
const getDetailDesc = formatMessage({
    id: 'api.message.getDetailDesc',
    defaultMessage: '查询潜客信息详情'
});
const exportDesc = formatMessage({
    id: 'api.message.queryExport',
    defaultMessage: '导出'
});
const getAbandonDesc = formatMessage({
    id: 'api.getAbandonDesc',
    defaultMessage: '作废'
});
//初始化
export const getInitData = () => request(`${baseUrl}/ui/potentialCustomer/init`, null, dto.InitDataStruct)
    .then(handleFetchResultNotification(REQUEST_TYPE.initQuery, getInitDesc));
// 查询列表
export const getList = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/potentialCustomers/forCurrentUser?${queryString}`, null, dto.List)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getListDesc));
};
// 查询详情
export const getDetail = id => request(`${baseUrl}/potentialCustomers/${id}`, null, dto.Detail)
    .then(handleFetchResultNotification(REQUEST_TYPE.query, getDetailDesc));

// 作废
export const abandonOrder = (id, rowVersion) => request(`${baseUrl}/potentialCustomers/${id}/abandon`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify({
        rowVersion
    })
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, getAbandonDesc));
//导入潜客信息模板
export const importTemp = () => request(`${baseUrl}/potentialCustomers/importTemp`)
    .then(handleFetchResultNotification(REQUEST_TYPE.query, null)).then(res => res.data);
//导出Excel
export const queryExport = queryCondition => {
    const queryString = stringify(queryCondition);
    return request(`${baseUrl}/potentialCustomers/export/forCurrentUser?${queryString}`)
        .then(handleFetchResultNotification(REQUEST_TYPE.submit, exportDesc))
        .then(res => res.data);
};
